import { gameConfigHandle } from "core/config/gameConfig"
import { ItemModel } from "core/model/itemModel"
import { PlayerModel } from "core/model/playerModel"
import { NormlaTaskType, EquipmentType, RewardType, TaskType, OccType } from "core/typeDefine/typeDefine"
import utils from "core/utils/utils"
import bagHandle from "./bagHandle"
import equipmentHandle from "./equipmentHandel"
import itemHandle from "./itemHandle"
import playerHandle from "./playerHandle"
import skillHandle from "./skillHandle"
import adventureHandle from "./adventureHandle"
import { EquipmentsLvlupModel } from "core/model/partnerModel"
import partnerHandle from "./partnerHandle"
import talentHandle from "./talentHandle"


class TaskHandle {
    //计算每次收益的经验/金币 (1分钟)
    getResourcePerTimes(player: PlayerModel, type: string) {
        let getResourceByFloor = gameConfigHandle.gameConfig.adventure.getResourceByFloor[type]
        let value = utils.getIncValues(getResourceByFloor, player.adventure.floor)
        return value
    }
    //获得任务奖励
    getTaskReward(player: PlayerModel, reward, rand) {
        let item: ItemModel = null
        let res = true
        if (reward.type == RewardType.Equipment) {
            item = equipmentHandle.create(player,null, rand, { equipmentType: reward.equipmentType, equipmentLvl: reward.lvl, quality: reward.quality })
            res = bagHandle.addItemComponent(player.equipmentBag, item)
        } else if (reward.type == RewardType.Prop) {
            res = bagHandle.addItem(player.bag, reward.tid, reward.stack)
        } else if (reward.type == RewardType.Resource) {
            let amount = reward.amount
            if (reward.tid == 'gold' || reward.tid == 'exp') {
                amount *= taskHandle.getResourcePerTimes(player, reward.tid)
            }
            player.itemInfo[reward.tid] += amount
        } else if (reward.type == RewardType.Skill) {
            // item = skillHandle.createBook(rand, { skillId: reward.skillId, quality: reward.quality, lSkills: reward.lSkills })
            item = skillHandle.createBook(rand, { skillId: reward.skillId, quality: reward.quality })
            res = bagHandle.addItemComponent(player.skillBag, item)
        }
        return res
    }

    //装备
    creatTaskEquipment(player: PlayerModel, index, rand) {
        if (index == 1) {//史诗 lv2法杖x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Weapon, equipmentLvl: 2, quality: 4 })
            return equipment
        } else if (index == 2) {//史诗 lv3法书x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.SubWeapon, equipmentLvl: 3, quality: 4 })
            return equipment
        } else if (index == 3) {//史诗 lv6帽子x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Head, equipmentLvl: 6, quality: 4 })
            return equipment
        } else if (index == 4) {//史诗 lv7衣服x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Body, equipmentLvl: 7, quality: 4 })
            return equipment
        } else if (index == 5) {//史诗 lv8耳环x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Earring, equipmentLvl: 8, quality: 4 })
            return equipment
        } else if (index == 6) {//史诗 lv9鞋子x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Foot, equipmentLvl: 9, quality: 4 })
            return equipment
        } else if (index == 7) {//史诗 lv10翅膀x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Wings, equipmentLvl: 10, quality: 4 })
            return equipment
        } else if (index == 8) {//史诗 lv15项链x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Necklace, equipmentLvl: 15, quality: 4 })
            return equipment
        } else if (index == 9) {//史诗 lv20戒指x1
            let equipment = equipmentHandle.create(player,null, rand, { equipmentType: EquipmentType.Ring, equipmentLvl: 20, quality: 4 })
            return equipment
        }
    }
    //材料
    creatTaskItem(index) {
        if (index == 1) {//lv1红宝石x2
            return itemHandle.create({ tid: 204003, stack: 2 })
        } else if (index == 2) {//lv2紫宝石x1
            return itemHandle.create({ tid: 204008, stack: 1 })
        } else if (index == 12) {//精石x2
            return itemHandle.create({ tid: 200001, stack: 2 })
        } else if (index == 14) {//精石x4
            return itemHandle.create({ tid: 200001, stack: 4 })
        } else if (index == 18) {//精石x8
            return itemHandle.create({ tid: 200001, stack: 8 })
        } else if (index == 116) {//精石x16
            return itemHandle.create({ tid: 200001, stack: 16 })
        } else if (index == 132) {//精石x32
            return itemHandle.create({ tid: 200001, stack: 16 })
        } else if (index == 22) {//技能碎片2
            return itemHandle.create({ tid: 200002, stack: 2 })
        } else if (index == 24) {//技能碎片4
            return itemHandle.create({ tid: 200002, stack: 4 })
        } else if (index == 28) {//技能碎片8
            return itemHandle.create({ tid: 200002, stack: 8 })
        } else if (index == 216) {//技能碎片16
            return itemHandle.create({ tid: 200002, stack: 16 })
        } else if (index == 31) {//乾坤石1
            return itemHandle.create({ tid: 200003, stack: 1 })
        } else if (index == 32) {//乾坤石2
            return itemHandle.create({ tid: 200003, stack: 2 })
        } else if (index == 33) {//乾坤石3
            return itemHandle.create({ tid: 200003, stack: 3 })
        } else if (index == 51) {//药剂1
            return itemHandle.create({ tid: 200005, stack: 1 })
        } else if (index == 52) {//药剂2
            return itemHandle.create({ tid: 200005, stack: 2 })
        } else if (index == 53) {//药剂3
            return itemHandle.create({ tid: 200005, stack: 3 })
        } else if (index == 54) {//药剂4
            return itemHandle.create({ tid: 200005, stack: 4 })
        } else if (index == 55) {//药剂5
            return itemHandle.create({ tid: 200005, stack: 5 })
        }
    }
    //技能
    creatTaskSkillBook(index, rand) {
        if (index == 1) {//优秀冰箭x1
            let skillBook = skillHandle.createBook(rand, { skillId: 270001, quality: 2, lSkills: [270101, 270102] })
            return skillBook
        }
    }

    totalTaskAmount() {
        let taskList = gameConfigHandle.gameConfig.task.list
        let amount = 0
        for (let i = 0; i < taskList.length; i++) {
            let taskItem = taskList[i]
            amount += taskItem.value.length
        }
        return amount
    }
    //是否有常规任务奖励可以领取
    canGetReward(player: PlayerModel) {
        let taskList = gameConfigHandle.gameConfig.task.list
        let canGet = false
        for (let i = 0; i < taskList.length; i++) {
            if (this.isTaskPass(player, taskList[i]).isPass) {
                canGet = true
                break
            }
        }
        return canGet
    }

    //判断日常任务是否完成
    isDayMessionPass(id: number, player: PlayerModel) {
        let dayMessions = gameConfigHandle.gameConfig.task.dayMessions
        let dayMession = dayMessions[id]
        if (dayMession.type == TaskType.MarketBuy) {
            if (player.market.buyDayAmount >= dayMession.value) {
                return true
            }
        } if (dayMession.type == TaskType.DaySign) {
            if (player.baseInfo.daySign) {
                return true
            }
        } else if (dayMession.type == TaskType.Instance) {
            let doneTimes = player.adventure.instanceAmount
            if (doneTimes >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.GetOnlineData) {
            let doneTimes = player.baseInfo.getOnlineDataTimes
            if (doneTimes >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.Abyss) {
            if (player.adventure.abyssAmount >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.ClickGold) {
            if (player.baseInfo.clickGetGoldTimes >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.Arena) {
            if (player.baseInfo.dayArenaTimes >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.Chest) {
            if (player.baseInfo.dayChestTimes >= 1) {
                return true
            }
        } else if (dayMession.type == TaskType.Share) {
            if (player.baseInfo.shareSign1 == true) {
                return true
            }
        }
        return false
    }

    //日常奖励可以领取的数量
    canGetDaymessionGift(player: PlayerModel) {
        let task = player.task
        let dayMessions = gameConfigHandle.gameConfig.task.dayMessions
        let giftAmount = 0
        //日常任务颗领奖数量统计
        for (let i = 0; i < dayMessions.length; i++) {
            if (this.isDayMessionPass(i, player) && !task.getDayMessionGift[i]) {
                giftAmount += 1
            }
        }
        //日常彩蛋可领取数量
        let progress = this.getDayMessionProgress(player).score
        let giftsOfEgg = gameConfigHandle.gameConfig.task.dayMessionEggGift
        for (let i = 0; i < giftsOfEgg.progress.length; i++) {
            if (progress >= giftsOfEgg.progress[i] && !task.dayMessionGiftsState[i]) {
                giftAmount += 1
            }
        }
        return giftAmount
    }

    isTaskPass(player: PlayerModel, taskItem: any) {
        let type = taskItem.type
        let index = player.task.list[type] || 0
        let values: number[] = taskItem.value
        let isComplete = false
        //如果该任务已经全部完成
        if (index >= values.length - 1) {
            isComplete = true
        }

        //是否达成领奖条件
        let conditionValue = values[index]
        if (type == NormlaTaskType.Altar) {
            if (player.altar.lvl >= conditionValue) {
                return { total: player.altar.lvl, isPass: true, isComplete }
            } else {
                return { total: player.altar.lvl, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.PartnerLvl) {
            let total = 0
            player.team.teamList.forEach(tid => {
                if (tid) {
                    let partner = playerHandle.getPartnerByIndexID(player, tid)
                    if (partner && partner.lvl) {
                        total += partner.lvl
                    }
                }
            })
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.AdventureFloor) {
            let { total, maxTotal } = adventureHandle.getAdventureMapProgress(player)
            if (maxTotal >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.ExploreFloor) {
            let total = adventureHandle.getAdventureExploreTotalTimes(player)
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.EquipmentStr) {
            let total = 0
            let equipmentConfig = gameConfigHandle.gameConfig.equipment
            Object.keys(player.team.recruitList).forEach(indexID => {
                let partner = player.team.recruitList[indexID]
                if (partner) {
                    equipmentConfig.equipmentListType.map(type => {
                        total += partner.equipmentsStr[type]
                    })
                }
            })
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.Instance) {
            let mapConfig = gameConfigHandle.gameConfig.map
            let total = adventureHandle.getInstanceTotalProgress(player)
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.AbyssMap) {
            let mapConfig = gameConfigHandle.gameConfig.map
            let total = player.adventure.mapProgress[mapConfig.abyssMapID] || 0
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.TalentLvl) {
            let total = player.talent.lvl
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.FormationLvl) {
            let total = player.formation.lvl
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.ResearchLvl) {
            let total = 0
            player.research.immuneList.forEach(lvl => {
                if (lvl) {
                    total += lvl
                }
            })
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.MarketBuyTimes) {
            let total = player.market.buyTotalAmount
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false }
            }
        } else if (type == NormlaTaskType.HeroStarLvl) {
            let total = partnerHandle.getHeroTotalStar(player)
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.TreasureStarLvl) {
            let total = partnerHandle.getTreasureTotalStar(player)
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.OpenChestTimes) {
            let total = player.baseInfo.openChestTimes
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        } else if (type == NormlaTaskType.AltarStrLvl) {
            let total = 0
            Object.keys(player.altar.attriLvls).forEach(key => {
                total += player.altar.attriLvls[key] || 0
            })
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        }else if (type == NormlaTaskType.GodLvl) {
            let total = talentHandle.getGodTotalLvl(player)
            if (total >= conditionValue) {
                return { total: total, isPass: true, isComplete }
            } else {
                return { total: total, isPass: false, isComplete }
            }
        }
        return { total: 0, isPass: false, isComplete }
    }

    getDayMessionProgress = (player: PlayerModel) => {
        let score = 0
        let dayMessions = gameConfigHandle.gameConfig.task.dayMessions
        let totalScore = dayMessions.length
        for (let i = 0; i < totalScore; i++) {
            if (this.isDayMessionPass(i, player)) {
                score++
            }
        }

        return { progress: Math.floor(score * 100 / totalScore), score }
    }
}

let taskHandle = new TaskHandle()

export default taskHandle